/*
 * Copyright (c) 2013 Eugene Kuzmenko
 *
 * Permission  is hereby granted, free of charge, to any person obtaining a copy
 * of this software and associated documentation files (the "Software"), to deal
 * in  the Software without restriction, including without limitation the rights
 * to  use,  copy,  modify,  merge, publish, distribute, sublicense, and/or sell
 * copies  of  the  Software,  and  to  permit  persons  to whom the Software is
 * furnished to do so, subject to the following conditions:
 *
 * The  above  copyright  notice and this permission notice shall be included in
 * all copies or substantial portions of the Software.
 *
 * THE  SOFTWARE  IS  PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
 * IMPLIED,  INCLUDING  BUT  NOT  LIMITED  TO THE WARRANTIES OF MERCHANTABILITY,
 * FITNESS  FOR  A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
 * AUTHORS  OR  COPYRIGHT  HOLDERS  BE  LIABLE  FOR  ANY CLAIM, DAMAGES OR OTHER
 * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
 * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
 * SOFTWARE.
 *
 * For more information, see <http://www.aljey.com>.
 */

//=../Button/AbstractButton

/**
 * The menu item.
 *
 * @author Eugene Kuzmenko <mail@aljey.com>
 * @class {MenuItem}
 * @extends {AbstractButton}
 */
Class.define('MenuItem', {

  extend: 'AbstractButton',

  slotHolder: 'a',

  /**
   * Have a menu item point to a given url.
   *
   * @property {string}
   */
  href: '#',

  /**
   * If true will make the menu item unclickable.
   *
   * @property {bool}
   */
  disabled: false,

  tpl: [
    '<li role="presentation"<%if(disabled){%> class="disabled"<%}%>>',
      '<a role="menuitem" tabindex="-1" href="<%=href%>"></a>',
    '</li>'
  ],

  /**
   * Changes the disabled state of a menu item.
   *
   * @param {bool} val If true will make the menu item unclickable.
   * @returns {MenuItem}
   */
  setDisabled: _static.Container.setter(function (val) {
    this.el[val ? 'addClass' : 'removeClass']('disabled');
  }, 'disabled', 'bool'),

  /**
   * Changes the "href" attribute of a menu item.
   *
   * @param {string} val
   * @returns {MenuItem}
   */
  setHref: _static.Container.setter(function (val) {
    this._link.attr('href', val);
  }, 'href', 'string', '#'),

  listeners: {
    render: function () {
      var me = this;
      me._link = me.items.renderTo.click(function (e) {
        if ('#' === me.href) {
          if (!me.disabled) {
            _.isFunction(me.handler) && me.handler();
            me.fire('click');
          }
          e.preventDefault();
        }
        me.disabled && e.preventDefault();
      });
    }
  }

});